var modifyOrAddFlag;

$(document)
		.ready(
				function() {
					$('#win').window('close');
					// 初始化时间
					// initailDate();
					// ESB节点下拉框
					$('#comboxEsbNode')
							.combobox(
									{
										url : BASE_URL_API
												+ '/systemLogLevelManagement/getEsbNodes?v='
												+ Math.random(),
										valueField : 'valueCode',
										textField : 'valueName',
										editable : false,
										method : 'get',
										onLoadSuccess : function(data) {
											var data = $('#comboxEsbNode').combobox('getData');
											$("#comboxEsbNode ").combobox('select',data[0].valueCode);
										},
										onSelect: function(record) {
											$("#comboxEsbNode").val(record.valueCode);
											search();
											//console.log(record);
										},
									});
					// 日志级别下拉框
					$('#comboxLogLevel').combobox(
							{
								url : BASE_URL_DATA_DICTIONARY_VALUE_SEARCH
										+ '/findByTypeCode?typeCode='
										+ DDTC_LOG_LEVEL + '&v='
										+ Math.random(),
								valueField : 'valueCode',
								textField : 'valueName',
								editable : false,
								method : 'get',
								loadFilter : function(data) {
									var allCheck = {
										'valueCode' : '',
										'valueName' : '全部'
									};
									data.unshift(allCheck);
									return data;
								}
							});

					$("#datagrid")
							.datagrid(
									{
										url : BASE_URL_API
												+ '/systemLogLevelManagement/findSystemLogLevelInfos?v='
												+ Math.random(),
										method : 'get',
										autoRowHeight : true,
										singleSelect : true,
										striped : true,
										nowrap : false,
										pageSize : 20,
										pageList : [ 10, 20, 50, 100 ],
										columns : [ [
												{
													field : 'receivedStr',
													title : '生成时间',
													width : 15,
													align : 'center'
												},
												{
													field : 'level',
													title : '级别',
													width : 10,
													align : 'center'
												},
												{
													field : 'message',
													title : '详细信息',
													width : 50,
													align : 'left'
												},
												{
													field : 'bundleName',
													title : 'Bundle名称',
													width : 60,
													align : 'left',
													formatter : function(value,
															rowData, rowIndex) {
														return '<a href="javascript:void(0)" class="default_btn fl"  onclick="showBundleDetail('
																+ rowData.bundleId
																+ ')">'
																+ value
																+ '</a>';

													}
												}, {
													field : 'exception',
													title : '异常信息',
													width : 60,
													align : 'left'
												} ] ],
										pagination : true,
										onSelect : function(rowIndex, rowData) {
										},
										onUnselect : function() {
										},
										loadFilter : function(data) {
											// alert("inData:"
											// + JSON.stringify(data));
											return {
												total : data.totalElements,
												rows : data.content
											};
										},
										onBeforeLoad : function(param) {
											param.page -= 1;
											param.size = param.rows;
											delete param.rows;

											param.sort = 'CREATED_DATE desc';

											var service = $('#txtServiceName')
													.val();
											if (service != "") {
												param.service = service;
											}
											var bundleName = $('#txtBundleName')
													.val();
											if (bundleName != "") {
												param.bundleName = bundleName;
											}
											var level = $('#comboxLogLevel')
													.combobox('getValue');
											if (level != "") {
												param.level = level;
											}
											var serviceMixIp = $(
													'#comboxEsbNode').combobox(
													'getValue');
											if (serviceMixIp != "") {
												param.serviceMixIp = serviceMixIp;
											}
										},
										onLoadError : function(e) {
											// alert(JSON.stringify(e));
											$.messager.alert('错误', '<p>'
													+ e.responseJSON.message
													+ '</p>', 'error');
										}
									});

					$("#datagrid").datagrid('getPager').pagination({
						displayMsg : '当前显示从 [{from}] 到 [{to}] 共[{total}]条记录'
					});
					$(".panel-tool-close, #instance_cancel").click(function(e) {
						$("body").css("overflow-x", "inherit")
					});

				});

function serviceLogExport() {
	initailDate();
	var serviceMixIp = $('#comboxEsbNode').combobox('getValue');
	var service = $('#txtServiceName').val();
	var bundleName = $('#txtBundleName').val();
	var level = $('#comboxLogLevel').combobox('getValue');

	location.href = BASE_URL_API
			+ '/systemLogLevelManagement/systemLogLevelExport?&serviceMixIp='
			+ serviceMixIp + '&service=' + service + '&bundleName='
			+ bundleName + '&level=' + level;
}

/*
 * function showBundleDetail(bundleId) { // alert(bundleId);
 * window.location.href =
 * "../ServiceGroupManagement/BaseServiceManagement.html?bundleId=" + bundleId +
 * '&v=' + Math.random(); }
 */

function showBundleDetail(bundleId) {
	$("#datagridDetail")
			.datagrid(
					{
						url : BASE_URL_API
								+ '/baseServiceManagement/findBundleDetail?v='
								+ Math.random(),
						method : 'get',
						autoRowHeight : true,
						singleSelect : true,
						striped : true,
						nowrap : false,
						model : true,

						shadow : true,
						columns : [ [
								{
									field : 'id',
									title : 'Id',
									width : 10,
									align : 'center',
									styler : function(value, row, index) {
										return {
											style : 'vertical-align: top;'
										}
									}
								},
								{
									field : 'name',
									title : 'Bundle名称',
									width : 150,
									align : 'center',
									formatter : function(value, rowData,
											rowIndex) {
										var companent = '<div width="auto"><ul>';
										companent += '<li><div width="auto" float="left" align="left"><label style="align:left;">'
												+ value + '</label></div></li>';
										companent += '<br/><li>'
												+ createPropsTable(rowData.props)
												+ '</li>'
										companent += '</ul></div>';
										return companent;

									}
								}, {
									field : 'version',
									title : '版本',
									width : 10,
									align : 'center',
									styler : function(value, row, index) {
										return {
											style : 'vertical-align: top;'
										}
									}
								}, {
									field : 'category',
									title : '类别',
									width : 10,
									align : 'center',
									styler : function(value, row, index) {
										return {
											style : 'vertical-align: top;'
										}
									}
								}, {
									field : 'state',
									title : '状态',
									width : 10,
									align : 'center',
									styler : function(value, row, index) {
										return {
											style : 'vertical-align: top;'
										}
									}
								} ] ],
						pagination : false,
						loadFilter : function(data) {
							// alert("inData:"
							// + JSON.stringify(data));
							return {
								total : data.totalElements,
								rows : data.content
							};
						},
						onBeforeLoad : function(param) {
							param.page -= 1;
							param.size = param.rows;
							delete param.rows;
							param.bundleId = bundleId;
						}
					});
	$(".panel-tool-close, #instance_cancel").click(function(e) {
		$("body").css("overflow-x", "inherit")
	});
	$("#datagridDetail").datagrid('reload');
	$('#win').window('setTitle', 'Bundle详情');
	$('#win').window('open');

}
function createPropsTable(props) {
	if (props == null || props.length == 0) {
		return "";
	}
	var table = '<table width="150px">';
	// var regExpLt = new RegExp('<', 'g');

	var regBR = new RegExp(',  ', 'g');
	var regBR2 = new RegExp(', !! ', 'g');
	$
			.each(
					props,
					function(index, item) {
						var value = "";
						if(item.value instanceof Array)
						{
							for(var i = 0; i < item.value.length; i++)
							{
								value += item.value[i] + "<br/>";
							}
						}
						else
						{
							value = item.value + "";
						}
						value = value.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
						value = value.replace(regBR, '<br/>');
						value = value.replace(regBR2, '<br/>');
						//value = value.replace(/,/g, "<br/>");
						value = value.replace(/href=\"([^\"])*\"/g, '');
						value = value.replace(/href=\'([^\'])*\'/g, '');
						value = value.replace(/<a/g, "<label");
						value = value.replace(/<\/a/g, "</label");
						var key = item.key + "";
						key = match(key);
						key = key.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
						table += '<tr><td width="auto"  align="left"  valign="top"><label>'
								+ key
								+ '</label></td><td  align="left" style="word-wrap:break-word;word-break:break-all;"  width="200px" height="auto"><div align="left" style="height: auto; width: 730px;"><label>'
								+ value + '</label></div></td>';
					});
	table += "</table>"
	return table;
}
function match(value) {
	if (value == '' || value == null) {
		return "";
	}
	var substr = value.match("&lt;\\s*a.*&gt;.*&lt;/\\s*a.*&gt;");
	if (substr == null) {
		return value;
	}
	var sublv1 = substr + "";
	// sublv1 = sublv1.substring(0, sublv1.lastIndexOf(key) + key.length);
	var substr2 = sublv1.match("&gt;.*&lt;");
	if (substr2 == null) {
		return value;
	}
	var sublv2 = substr2 + "";
	// sublv2 = sublv2.substring(1, sublv2.length-1);
	sublv2 = sublv2.replace("&gt;", '');
	sublv2 = sublv2.replace("&lt;", '');
	var result = value.replace(sublv1, sublv2);
	return result;
}
function initailDate() {

	var curr_time = new Date();
	var year = curr_time.getFullYear();
	var month = curr_time.getMonth() + 1;
	var day = curr_time.getDate();
	var hours = curr_time.getHours();
	var minutes = curr_time.getMinutes();
	var seconds = curr_time.getSeconds();
	if (month < 10) {
		month = '0' + month;
	}
	if (day < 10) {
		day = '0' + day;
	}
	if (hours < 10) {
		hours = '0' + hours;
	}
	if (minutes < 10) {
		minutes = '0' + minutes;
	}
	if (seconds < 10) {
		seconds = '0' + seconds;
	}
	var start = year + '-' + month + '-' + day + ' 00:00:00';
	var end = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
			+ ':' + seconds;

	$("#txtStartReceivedTime").datetimebox("setValue", start);
	$("#txtEndReceivedTime").datetimebox("setValue", end);
}

// 查询
function search() {
	$('#datagrid').datagrid('reload');
}
function refresh() {
	$('#datagrid').datagrid('reload');
}

var loading_point = {
	timer : null,
	len : 3,
	count : 0
};
function loading() {
	if (loading_point.count < loading_point.len) {
		loading_point.count++;
		$("#progressbar_point").text($("#progressbar_point").text() + ".");
	} else {
		loading_point.count = 0;
		$("#progressbar_point").text("");
	}
}
